<?php
include_once dirname(__FILE__).'/../RDBConfig.php';
include_once dirname(__FILE__).'/../../interface/RequirementDAOInterface.php';
include_once dirname(__FILE__).'/../../object/Requirement.php';

class RequirementDAO implements RequirementDAOInterface {
	
	public static $TABLE_NAME = 'REQUIREMENT';
	public static $COL_ARTIFACT_PK = 'ARTIFACT_PK';
	public static $COL_FOLDER_PK  = 'FOLDER_PK';
	public static $COL_ARTIFACT_NAME  = 'ARTIFACT_NAME';
	
	public function __construct() {
	
	}
	
	/**
	 * Tra ve cac Requirement cua Folder co PK tuong ung.
	 * @see RequiremetnDAOInterface::getRequirementByFolder()
	 * @param ProjectFolder $folder
	 * @return Requirement (array)
	 */
	public function getRequirementByFolder($folder) {
		$table = self::$TABLE_NAME;
		$colArtifactPK = self::$COL_ARTIFACT_PK;
		$colFolderPK = self::$COL_FOLDER_PK;
		$colArtifactName = self::$COL_ARTIFACT_NAME;
		
		$tableFolder = ProjectFolderDAO::$TABLE_NAME;
		
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
		
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        SELECT a.$colArtifactPK, a.$colFolderPK, a.$colArtifactName
        FROM $table a, $tableFolder b
        WHERE 	a.$colFolderPK = b.$colFolderPK
        	AND	b.$colFolderPK = :folder_pk
END;
		
		$data = array( // chuan bi du lieu
				':folder_pk' => $folder->getFolderPK()
		);
		
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$artifacts = array();
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			foreach ($sql->fetchAll() as $req) {
				$artifacts[] = new Requirement($req["$colArtifactPK"],
						$req["$colFolderPK"], $req["$colArtifactName"]);
			}
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
		
		return $artifacts;
	}
	
	/**
	 * Tra ve cac Requirement cua Project co PK tuong ung.
	 * @see RequiremetnDAOInterface::getRequirementByProject()
	 * @param Project $project
	 * @return Requirement (array)
	 */
	public function getRequirementByProject($project) {
		$table = self::$TABLE_NAME;
		$colArtifactPK = self::$COL_ARTIFACT_PK;
		$colFolderPK = self::$COL_FOLDER_PK;
		$colArtifactName = self::$COL_ARTIFACT_NAME;
	
		$tableFolder = ProjectFolderDAO::$TABLE_NAME;
	
		$tableProject = ProjectDAO::$TABLE_NAME;
		$colProjectPK = ProjectDAO::$COL_PROJECT_PK;
		// tao ket noi
		$rdbConfig = new RDBConfig(RDBConfig::$DEFAULT_HOST, RDBConfig::$DEFAULT_DBNAME,
				RDBConfig::$DEFAULT_USERNAME, RDBConfig::$DEFAULT_PASSWORD);
		$conn = RDBDAOFactory::createConnection($rdbConfig);
	
		// tao cau truy van (luu y chu END; cuoi khong co dau cach phia truoc!)
		$statement = <<<END
        SELECT a.$colArtifactPK, a.$colFolderPK, a.$colArtifactName
        FROM $table a, $tableFolder b, $tableProject c
        WHERE 	a.$colFolderPK = b.$colFolderPK
        	AND	b.$colProjectPK = c.$colProjectPK
        	AND c.$colProjectPK = :project_pk 
END;
	
		$data = array( // chuan bi du lieu
				':project_pk' => $project->getProjectPK()
		);
	
		$sql = $conn->prepare($statement); // chuan bi cau truy van
		$artifacts = array();
		try {
			$sql->execute($data); // do du lieu vao cau truy van
			$sql->setFetchMode(PDO::FETCH_ASSOC);
			foreach ($sql->fetchAll() as $req) {
				$artifacts[] = new Requirement($req["$colArtifactPK"],
						$req["$colFolderPK"], $req["$colArtifactName"]);
			}
		} catch (PDOException $excs) { // neu co loi thi cuon lai
			echo $excs->getMessage().'<br>';
		}
		$conn = null; // free memory
	
		return $artifacts;
	}
}
?>
